package com.tsd.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tsd.CommonMsgNameConstants;
import com.tsd.CommonParamNameConstants;
import com.tsd.common.controller.BaseController;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysDataAuth;
import com.tsd.system.entity.SysDataAuthExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysDataAuthService;
import com.tsd.system.utils.AuthViewFieldUtil;
import com.tsd.utils.DataFieldAuthUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysDataAuth数据授权 控制层
 *
 * @author admin
 * @date 2022/11/15 15:12:44
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "系统-数据授权", description = "数据授权的API描述")
public class SysDataAuthController extends BaseController {

    @Resource
    private SysDataAuthService sysDataAuthService;

    @ApiOperation(value = "根据sid获取数据授权", notes = "根据sid获取数据授权")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "数据授权sid", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getSysDataAuthBySid.action", method = RequestMethod.POST)
    public HttpResult<Object> getSysDataAuthBySid() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map, CommonParamNameConstants.SID);
            super.checkEmpty(sid, CommonMsgNameConstants.EMPTY_MSG_SID);
            SysDataAuth sysDataAuth = sysDataAuthService.queryBySid(sid);
            hr.setData(sysDataAuth);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除数据授权", notes = "根据id删除数据授权")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sids", value = "数据授权sids", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteSysDataAuths.action", method = RequestMethod.POST)
    public HttpResult<Object> deleteSysDataAuths() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object sids = map.get(CommonParamNameConstants.SIDS);
            super.checkEmpty(sids, CommonMsgNameConstants.EMPTY_MSG_SIDS);
            List<String> list = JSON.parseArray(JSON.toJSONString(sids), String.class);
            sysDataAuthService.deleteSysDataAuths(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存数据授权", notes = "保存数据授权，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "数据授权", required = true, paramType = "body", dataType = "SysDataAuth")
    })
    @ResponseBody
    @RequestMapping(value = "/saveSysDataAuth.action", method = RequestMethod.POST)
    public HttpResult<Object> saveSysDataAuth() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            SysDataAuthExt t = JSON.parseObject(json, SysDataAuthExt.class);
            sysDataAuthService.saveSysDataAuth(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "批量保存数据授权", notes = "批量保存数据授权")
    @ResponseBody
    @RequestMapping(value = "/bathSaveSysDataAuth.action", method = RequestMethod.POST)
    public HttpResult<Object> bathSaveSysDataAuth() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            List<SysDataAuthExt> list = super.getParamsList(CommonParamNameConstants.LIST, SysDataAuthExt.class, true);
            sysDataAuthService.bathSaveSysDataAuth(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询数据授权", notes = "分页查询数据授权")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findSysDataAuths.action", method = RequestMethod.POST)
    public HttpResult<Object> findSysDataAuths() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, CommonParamNameConstants.DR, false, 0);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.SEARCH_VALUE, false, true);
            PageBean page = new PageBean(map);
            List<SysDataAuthExt> list = sysDataAuthService.findSysDataAuths(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "账号管理查询数据授权", notes = "账号管理查询数据授权")
    @ResponseBody
    @RequestMapping(value = "/findSysDataAuths4User.action", method = RequestMethod.POST)
    public HttpResult<Object> findSysDataAuths4User() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, CommonParamNameConstants.DR, false, 0);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.AUTH_TYPE, true, false);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.USER_SID, true, false);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.TYPE, false, false);
            List<SysDataAuthExt> list = sysDataAuthService.findSysDataAuths4User(params);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "获取用户数据授权", notes = "获取用户数据授权")
    @ResponseBody
    @RequestMapping(value = "/checkSysDataAuth4UserByCode.action", method = RequestMethod.POST)
    public HttpResult<Object> checkSysDataAuth4UserByCode() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String code = HlpUtils.getString(map, CommonParamNameConstants.CODE);
            String data = HlpUtils.getString(map, CommonParamNameConstants.DATA);
            super.checkEmpty(code, CommonMsgNameConstants.getEmptyMsg(CommonParamNameConstants.CODE));
            SysDataAuthExt authExt = DataFieldAuthUtil.getSysDataAuthField(code, opUser);
            JSONObject object = new JSONObject();
            if (!HlpUtils.isEmpty(data)) {
                JSONObject jsonObject = JSON.parseObject(data);
                boolean checkAuth = AuthViewFieldUtil.checkAuthViewField(authExt, jsonObject, opUser);
                object.put(CommonParamNameConstants.AUTH, checkAuth);
            }
            object.put(CommonParamNameConstants.DATA_AUTH, authExt);
            hr.setData(object);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
